home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 1 / PC Actual CD 01.iso / share / dos / graficos / plydat14.arj / TWISTWIR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  6.8 KB  |  241 lines

  1. /*
  2.  * twistwir.c - Create a twisted wire pair
  3.  *
  4.  * Alexander Enzmann
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #ifdef MAC
  11. #include <console.h>
  12. #endif
  13. #include "def.h"
  14. #include "lib.h"
  15.  
  16. static void
  17. determine_coil_point(COORD4 *pos, COORD4 *norm,
  18.              double theta, double phi,
  19.              double r0, double r1, double d0,
  20.              int left_right_flag, double ang_offset)
  21. {
  22.    COORD4 v0, v1, vd;
  23.    double len;
  24.  
  25.    /* Does this helix turn left or right? */
  26.    if (left_right_flag) theta = -theta;
  27.  
  28.    /* Add in any angle offset */
  29.    theta += ang_offset;
  30.  
  31.    /* Center of the coil */
  32.    SET_COORD4(v0, r0 * cos(theta), r0 * sin(theta), d0, 1.0);
  33.  
  34.    /* Point on the coil */
  35.    SET_COORD4(*pos, (r0 + r1 * sin(phi)) * cos(theta),
  36.             (r0 + r1 * sin(phi)) * sin(theta),
  37.             (r1 * cos(phi)) + d0,
  38.             1.0);
  39.  
  40.    /* Direction from center to point */
  41.    SUB3_COORD(vd, *pos, v0);
  42.    len = lib_normalize_coord3(&vd);
  43.  
  44.    v0.x = r1*cos(phi)*cos(theta);
  45.    v1.x = -(r0 + r1*sin(phi))*sin(theta);
  46.  
  47.    v0.y = r1*cos(phi)*sin(theta);
  48.    v1.y = (r0 + r1*sin(phi))*cos(theta);
  49.  
  50.    v0.z = -r1*sin(phi);
  51.    v1.z = 0.0;
  52.  
  53.    CROSS(*norm, v0, v1);
  54.    len = lib_normalize_coord3(norm);
  55.  
  56.    len = DOT_PRODUCT(*norm, vd);
  57.    if (len < 0.0) {
  58.       norm->x *= -1.0;
  59.       norm->y *= -1.0;
  60.       norm->z *= -1.0;
  61.       }
  62.    norm->w = 0.0;
  63. }
  64.  
  65. static void
  66. generate_coil(COORD4 *start, COORD4 *end,
  67.          int turns, int step_per_turn, int step_per_side,
  68.          double r0, double r1, int lrflag, double ang_offset,
  69.          char *txname)
  70. {
  71.    int i, j, k;
  72.    COORD4 p0, p[4], n0, n[4];
  73.    COORD4 dir;
  74.    MATRIX trans;
  75.    double dist, u0, u1, v0, v1, d0, d1, len;
  76.    double deltad, deltau, deltav;
  77.  
  78.    SUB3_COORD(dir, *end, *start);
  79.    dist = lib_normalize_coord3(&dir);
  80.  
  81.    /* Coils are sorta complex, so make bounding slabs oriented along
  82.       the direction of the coil */
  83.    lib_output_bounding_slab(&dir);
  84.  
  85.    /* Figure out the transform to get from the parametrically defined
  86.       coil to the start-end line */
  87.    lib_create_canonical_matrix(trans, start, &dir);
  88.  
  89.    deltad = dist / (double)(turns * step_per_turn);
  90.    deltau = 2.0 * PI / (double)step_per_turn;
  91.    deltav = 2.0 * PI / (double)step_per_side;
  92.  
  93.    /* Start generating the triangles in the coil */
  94.    for (i=0,d0=0.0;i<turns;i++) {
  95.       for (j=0;j<step_per_turn;j++,d0+=deltad) {
  96.          d1 = d0 + deltad;
  97.      u0 = (double)j * deltau;
  98.      u1 = u0 + deltau;
  99.      for (k=0;k<step_per_side;k++) {
  100.         v0 = (double)k * deltav;
  101.         v1 = v0 + deltav;
  102.  
  103.         /* Make the vertices of this patch on the coil */
  104.         determine_coil_point(&p0, &n0, u0, v0, r0, r1, d0,
  105.                  lrflag, ang_offset);
  106.         lib_transform_coord(&p[0], &p0, trans);
  107.         lib_transform_coord(&n[0], &n0, trans);
  108.         determine_coil_point(&p0, &n0, u0, v1, r0, r1, d0,
  109.                  lrflag, ang_offset);
  110.         lib_transform_coord(&p[1], &p0, trans);
  111.         lib_transform_coord(&n[1], &n0, trans);
  112.         determine_coil_point(&p0, &n0, u1, v1, r0, r1, d1,
  113.                  lrflag, ang_offset);
  114.         lib_transform_coord(&p[2], &p0, trans);
  115.         lib_transform_coord(&n[2], &n0, trans);
  116.  
  117.         /* Make triangles out of the vertices */
  118.         /* lib_output_polygon(3, &p[0], txname); */
  119.         lib_output_polypatch(3, &p[0], &n[0], txname);
  120.  
  121.         COPY_COORD4(p[1], p[2]); COPY_COORD4(n[1], n[2]);
  122.         determine_coil_point(&p0, &n0, u1, v0, r0, r1, d1,
  123.                  lrflag, ang_offset);
  124.         lib_transform_coord(&p[2], &p0, trans);
  125.         lib_transform_coord(&n[2], &n0, trans);
  126.         /* lib_output_polygon(3, &p[0], txname); */
  127.         lib_output_polypatch(3, &p[0], &n[0], txname);
  128.         }
  129.      }
  130.       }
  131. }
  132.  
  133. static void
  134. generate_cyl_coil(COORD4 *start, COORD4 *end,
  135.          int turns, int step_per_turn,
  136.          double r0, double r1,
  137.          int lrflag, double ang_offset,
  138.          char *txname)
  139. {
  140.    int i, j, k;
  141.    COORD4 p, p0, p1, n0;
  142.    COORD4 dir;
  143.    MATRIX trans;
  144.    double dist, u0, u1, d0, d1, len;
  145.    double deltad, deltau;
  146.  
  147.    SUB3_COORD(dir, *end, *start);
  148.    dist = lib_normalize_coord3(&dir);
  149.  
  150.    /* Coils are sorta complex, so make bounding slabs oriented along
  151.       the direction of the coil */
  152.    lib_output_bounding_slab(&dir);
  153.  
  154.    /* Figure out the transform to get from the parametrically defined
  155.       coil to the start-end line */
  156.    lib_create_canonical_matrix(trans, start, &dir);
  157.  
  158.    deltad = dist / (double)(turns * step_per_turn);
  159.    deltau = 2.0 * PI / (double)step_per_turn;
  160.  
  161.    /* Start generating the cylinders making up the coil */
  162.    for (i=0,d0=0.0;i<turns;i++) {
  163.       for (j=0;j<step_per_turn;j++,d0+=deltad) {
  164.          d1 = d0 + deltad;
  165.      u0 = (double)j * deltau;
  166.      u1 = u0 + deltau;
  167.      determine_coil_point(&p, &n0, u0, 0.0, r0, 0.0, d0,
  168.                   lrflag, ang_offset);
  169.      lib_transform_coord(&p0, &p, trans);
  170.      p0.w = r1;
  171.      determine_coil_point(&p, &n0, u1, 0.0, r0, 0.0, d1,
  172.                   lrflag, ang_offset);
  173.      lib_transform_coord(&p1, &p, trans);
  174.      p1.w = r1;
  175.      lib_output_cylcone(&p0, &p1, txname);
  176.      lib_output_sphere(&p0, txname);
  177.      }
  178.       }
  179.    /* Cap the end */
  180.    lib_output_sphere(&p1, txname);
  181. }
  182.  
  183. void
  184. main(int argc, char *argv[])
  185. {
  186.     COORD4  back_color, coil_color, dir;
  187.     COORD4  center_pt, end_pt, light;
  188.     COORD4  from, at, up;
  189.     char *txname;
  190.  
  191.     MATRIX m1, m2;
  192.  
  193. #ifdef MAC
  194.    argc = ccommand(&argv);
  195. #endif
  196.  
  197.     /* output viewpoint */
  198.     SET_COORD(from, 0.0, 2.0,-12.0);
  199.     SET_COORD(at,   0.0, 0.0, 5.0);
  200.     SET_COORD(up,   0.0, 1.0, 0.0);
  201.     lib_output_viewpoint( &from, &at, &up, 30.0, 1.0, 0.5, 128, 256);
  202.  
  203.     /* output background color - dark blue */
  204.     SET_COORD( back_color, 0.039, 0.18, 0.376 ) ;
  205.     lib_output_background_color( &back_color ) ;
  206.  
  207.     /* Output bounding slabs oriented along the coordinate axes */
  208.     SET_COORD(dir, 1.0, 0.0, 0.0);
  209.     lib_output_bounding_slab(&dir);
  210.     SET_COORD(dir, 0.0, 1.0, 0.0);
  211.     lib_output_bounding_slab(&dir);
  212.     SET_COORD(dir, 0.0, 0.0, 1.0);
  213.     lib_output_bounding_slab(&dir);
  214.  
  215.     /* output light source */
  216.     SET_COORD( light,-10.0, 10.0,-20.0);
  217.     lib_output_light( &light);
  218.     /*
  219.     SET_COORD( light, 10.0, 10.0,-20.0);
  220.     lib_output_light( &light);
  221.     */
  222.  
  223.     /* output coil color - red */
  224.     SET_COORD(coil_color, 1.0, 0.2, 0.2 );
  225.     txname = lib_output_color(&coil_color, 0.2, 0.8, 0.4, 0.0, 0.0, 0.0, 0.0);
  226.  
  227.     /* compute and output coil */
  228.     SET_COORD4(center_pt,0.0,-3, 0.0, 1.0);
  229.     SET_COORD4(end_pt, 0.0, 3, 0.0, 1.0);
  230.     generate_cyl_coil(¢er_pt, &end_pt, 3, 12, 0.1, 0.1, 0, 0.0, txname);
  231.  
  232.     /* output coil color - grey */
  233.     SET_COORD(coil_color, 0.3, 0.3, 0.3);
  234.     txname = lib_output_color(&coil_color, 0.2, 0.8, 0.1, 0.0, 0.0, 0.0, 0.0);
  235.  
  236.     /* compute and output coil */
  237.     SET_COORD4(center_pt, 0.0,-3, 0.0, 1.0);
  238.     SET_COORD4(end_pt, 0.0, 3, 0.0, 1.0);
  239.     generate_cyl_coil(¢er_pt, &end_pt, 3, 12, 0.1, 0.1, 0, PI, txname);
  240. }
  241.